/*
 * txdnet.cn tonydon
 * by netbeans ide 7.2
 */
package cn.txdnet.txweb.app;

import cn.txdnet.txcms.auth.AdminRoleAccessConfig;
import cn.txdnet.txweb.cache.TxCacheManager;
import cn.txdnet.txweb.mail.EmailConfig;
import cn.txdnet.txweb.model.ModelHandler;
import cn.txdnet.txweb.utils.BeanUtil;
import cn.txdnet.txweb.utils.SiteActionMgr;
import cn.txdnet.txweb.utils.StringUtil;
import cn.txdnet.txweb.utils.VelocityUtil;
import java.util.Properties;
import txdnet.util.DataUtil;
import txdnet.util.FileUtil;
import txdnet.util.Logger;
import txdnet.util.StaticMark;

/**
 * 用于系统模块加载：action, mail, velocity, jspview
 *
 * @author tangxiaodong
 */
public class ModuleConfig {

    private static Class clazz = ModuleConfig.class;

    public static void init() throws Exception {
        Properties p = FileUtil.readPropertiesFile(clazz.getResourceAsStream("/txweb.properties"));
        initAction(p);
        boolean velocityEnabled = initVelocity(p);
        boolean jspViewEnabled = initJspView(p);
        if (!velocityEnabled && !jspViewEnabled) {
            throw new RuntimeException("Velocity Or JspView , One of them must be configured.");
        }
        initMailSmtp(p);
        initAdminRoleAccess(p);
        initCacheManager(p);
    }

    /**
     * 初始化Action Url Map
     *
     * @param p
     * @throws Throwable
     */
    private static void initAction(Properties p) throws Exception {
        // url action 定义文件
        String urlaction = p.getProperty("controller.urlconf.files", null);
        if (urlaction != null) {
            try {
                // 初始化action class
                SiteActionMgr.initActionUrlMap(StringUtil.split(urlaction, StaticMark.CHAR_COMMA));
                Logger.info(clazz, "Init Action Url Map. OK.");
            } catch (Exception e) {
                Logger.error(clazz, "Init Action Url Map Error.");
                throw new RuntimeException(e);
            }
        } else {
            Logger.error(clazz, "Init Error, Not Config Url Action.");
        }
    }

    /**
     * 初始化模版引起
     *
     * @param p
     */
    private static boolean initVelocity(Properties p) {
        // 是否启用velocity模板引擎
        String velocity = p.getProperty("controller.velocity.enabled", null);
        // 初始化velocity
        if (velocity != null && velocity.equals("true")) {
            VelocityUtil.init();
            Logger.info(clazz, "Init Velocity Config. OK.");
            return true;
        }
        Logger.info(clazz, "Velocity Not Config.");
        return false;
    }

    /**
     * 初始化JSP视图参数
     *
     * @param p
     * @return
     */
    private static boolean initJspView(Properties p) {
        // jsp视图文件目录
        String jspdir = p.getProperty("controller.view.jspdir", null);
        // 初始化jsp view dir
        if (DataUtil.isEmpty(jspdir)) {
            Logger.info(clazz, "JspView Not Config.");
            return false;
        }
        ModelHandler.JSP_VIEW_DIR = jspdir;
        Logger.info(clazz, "Init JspViewDir Config. OK.");
        return true;
    }

    /**
     * 初始化邮件发送配置信息
     *
     * @param p
     */
    private static boolean initMailSmtp(Properties p) {
        // 是否启用mailsmtp功能
        boolean mailEnabled = Boolean.parseBoolean(p.getProperty("txweb.mail.enabled", "false"));
        if (mailEnabled) {
            EmailConfig.initSmtpMailConf();
            Logger.info(clazz, "Init Mail Sender Config. OK.");
        }
        return mailEnabled;
    }

    /**
     * 初始化管理员对action.method访问权限配置
     * @param p 
     */
    private static void initAdminRoleAccess(Properties p) {
        String roleConfFile = p.getProperty("txweb.admin.roleAccess.roleconf", null);
        AdminRoleAccessConfig.init(roleConfFile);
    }
    
    /**
     * 初始化缓存管理器
     */
    private static void initCacheManager(Properties p){
        String cacheProviderClass = p.getProperty("txweb.cache.provider.class", 
                "cn.txdnet.txweb.cache.ehcache.EhCacheProvider");
        TxCacheManager.initCacheProvider(cacheProviderClass);
        Logger.info(clazz, "TxCacheManager InitCacheProvider. OK. - ".concat(cacheProviderClass));
    }

    /**
     * 销毁资源
     */
    public static void destroy() {
        Logger.info(clazz, "Destroy Module Config...");
        SiteActionMgr.destroy();
        BeanUtil.destroy();
        EmailConfig.destroy();
        AdminRoleAccessConfig.destroy();
        TxCacheManager.shutdown();
    }
}
